package server.life;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import redis.clients.jedis.Jedis;
import server.img.MapleWZImg;
import tools.JsonUtil;
import tools.RedisUtil;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MobSkillFactory {

    private static final Logger log = LogManager.getLogger(MobSkillFactory.class.getName());
    private static final Map<String, MobSkill> allMobSkills = new HashMap<>();

    private MobSkillFactory() {
        initialize();
    }

    /*
     * 加载怪物技能信息
     */
    public static void initialize() {
        MapleDataProvider dataSource = MapleWZImg.INSTANCE.getSkill();
        MapleData skillRoot = dataSource.getData("MobSkill.img");
        for (MapleData skillData : skillRoot.getChildren()) {
            for (MapleData levelData : skillData.getChildByPath("level").getChildren()) {
                int skillId = Integer.parseInt(skillData.getName());
                int level = Integer.parseInt(levelData.getName());
                List<Integer> toSummon = new ArrayList<>();
                for (int i = 0; i <= 200; i++) { //暂时只循环200次 哪有召唤那么多的怪物
                    if (levelData.getChildByPath(String.valueOf(i)) == null) {
                        break;
                    }
                    toSummon.add(MapleDataTool.getInt(levelData.getChildByPath(String.valueOf(i)), 0));
                }
                MapleData ltdata = levelData.getChildByPath("lt");
                Point lt = null;
                if (ltdata != null) {
                    lt = (Point) ltdata.getData();
                }
                MapleData rbdata = levelData.getChildByPath("rb");
                Point rb = null;
                if (rbdata != null) {
                    rb = (Point) rbdata.getData();
                }
                MobSkill ret = new MobSkill(skillId, level);
                ret.addSummons(toSummon);
                ret.setCoolTime(MapleDataTool.getInt("interval", levelData, 0) * 1000);
                ret.setDuration(MapleDataTool.getInt("time", levelData, 0) * 1000);
                ret.setHp(MapleDataTool.getInt("hp", levelData, 100));
                ret.setMpCon(MapleDataTool.getInt("mpCon", levelData, 0));
                ret.setSpawnEffect(MapleDataTool.getInt("summonEffect", levelData, 0));
                ret.setX(MapleDataTool.getInt("x", levelData, 1));
                ret.setY(MapleDataTool.getInt("y", levelData, 1));
                ret.setProp(MapleDataTool.getInt("prop", levelData, 100) / 100f);
                ret.setLimit((short) MapleDataTool.getInt("limit", levelData, 0));
                ret.setOnce(MapleDataTool.getInt("summonOnce", levelData, 0) > 0);
                ret.setLtRb(lt, rb);
                ret.setAreaSequenceDelay(MapleDataTool.getInt("areaSequenceDelay", levelData, 0));
                ret.setSkillAfter(MapleDataTool.getInt("skillAfter", levelData, 0));
                ret.setForce(MapleDataTool.getInt("force", levelData, 0));
                ret.setForcex(MapleDataTool.getInt("forcex", levelData, 0));
                allMobSkills.put(skillId + ":" + level, ret);
            }
        }
        log.info("共加载 " + allMobSkills.size() + " 个怪物技能信息...");
    }

    /*
     * 通过技能ID 和 等级 获取怪物的技能信息
     */
    public static MobSkill getMobSkill(int skillId, int level) {
        return allMobSkills.get(skillId + ":" + level);
    }
}
